package com.app.micaihu.view.home.focusnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.bean.TopNewsGroup;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;

import java.util.List;

import jaydenxiao.com.expandabletextview.ExpandableTextView;

/**
 * 这是普通的分组Adapter 每一个组都有头部、尾部和子项。
 */
public class FocusNewsListAdapter extends GroupedRecyclerViewAdapter {

    protected List<TopNewsGroup> mGroups;

    public FocusNewsListAdapter(Context context) {
        super(context);
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<TopNewsGroup.TopNewsChild> children = mGroups.get(groupPosition).getNews();
        return children == null ? 0 : children.size();
    }

    public void clear() {
        mGroups.clear();
        notifyDataChanged();
    }

    public void setGroups(List<TopNewsGroup> groups) {
        mGroups = groups;
        notifyDataChanged();
    }

    public void addGroups(List<TopNewsGroup> groups) {
        if (mGroups != null) {
            mGroups.addAll(groups);
        }
        notifyDataChanged();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.focus_news_adapter_header;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.focus_news_adapter_header;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.focus_news_list_item;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        TopNewsGroup entity = mGroups.get(groupPosition);
        holder.setText(R.id.tvHeader, entity.getDate());
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
//        TopNewsGroup entity = mGroups.get(groupPosition);
//        holder.setText(R.id.tv_footer, entity.getFooter());
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        MobclickAgentUtils.event(UmEventId.importantnews_exposed, "要闻列表_曝光");

        TopNewsGroup.TopNewsChild entity = mGroups.get(groupPosition).getNews().get(childPosition);
        holder.setText(R.id.tvTime, entity.getArticleTime());
        holder.setText(R.id.tvTitle, entity.getArticleTitle());
        ExpandableTextView expandableTextView = holder.get(R.id.tvDesc);
        int position = groupPosition * 1000 + childPosition;
        expandableTextView.setText(entity.getArticleSummary(), position);
        if (StringUtils.isEmpty(entity.getArticleSummary())) {
            expandableTextView.setVisibility(View.GONE);
        } else {
            expandableTextView.setVisibility(View.VISIBLE);
        }
        expandableTextView.setOnExpandStateChangeListener(new ExpandableTextView.OnExpandStateChangeListener() {
            @Override
            public void onExpandStateChanged(TextView textView, boolean isExpanded) {
                if (isExpanded) {
                    MobclickAgentUtils.event(UmEventId.importantnews_click, "要闻列表_点击");
                }
            }
        });
    }

    @Override
    public View getEmptyView(ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(R.layout.view_empty_view, parent, false);

    }
}
